/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.device.servlet;

import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiendpointbean;
import cn.net.greenet.db.bean.Policyrepositoryinstance;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author hedingwei
 */
@WebServlet(urlPatterns = {"/device/get"})
public class Get extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("id");
        PrintWriter out = response.getWriter();
        if (id != null && id.matches("[0-9]*")) {
            SessionFactory sf = HibernateUtil.getSessionFactory();
            Session session = sf.openSession();
            session.clear();
            Dpiendpointbean dpi = (Dpiendpointbean) session.get(Dpiendpointbean.class, Integer.parseInt(id));
            JSONObject rst = new JSONObject();
            rst.put("Dev_Name", dpi.getName());
            rst.put("Dev_IP", dpi.getMasterIp());
            rst.put("Probe_Type", dpi.getProbeType() + "");
            rst.put("DeploySiteName", dpi.getDeploySiteName());
            rst.put("IDC_HouseID", dpi.getIdcHouseId());
            rst.put("description", dpi.getDescription());
            Set s = dpi.getPolicyrepositoryinstances();
            JSONArray ps = new JSONArray();
            Policyrepositoryinstance tmp = null;
            for (Object o : s) {
                tmp = (Policyrepositoryinstance) o;
                JSONObject obj = new JSONObject();
                obj.put("strategy_id", tmp.getId());
                obj.put("Message_Type", tmp.getPolicyrepositorytype().getMessageType());
                obj.put("strategy_name", tmp.getInstanceName());
                ps.add(obj);
            }
            rst.put("result", ps);
            session.close();
            out.print(rst);
        } else {
            out.println("[" + id + "]");
        }
        out.close();

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
